package org.ruiyichen.guizhou.agr.ui.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;

import org.ruiyichen.guizhou.agr.R;
import org.ruiyichen.guizhou.agr.base.BaseActivity;
import org.ruiyichen.guizhou.agr.base.DataManger;
import org.ruiyichen.guizhou.agr.bean.Response;
import org.ruiyichen.guizhou.agr.config.Constant;
import org.ruiyichen.guizhou.agr.imp.IIrrigatedAreaFilter;
import org.ruiyichen.guizhou.agr.net.OkHttpManager;
import org.ruiyichen.guizhou.agr.ui.mineview.BToast;
import org.ruiyichen.guizhou.agr.utils.StrUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 找回密码
 */
public class RetrievePasswordSecondActivity extends BaseActivity implements View.OnClickListener {

    public static final String PHONE_NUMBER = "phoneNumber";

    TextView tv_login_send_code;

    EditText tv_login_code,
            et_login_password;
    /**
     * 倒计时
     */
    private TimeCount mTimeCount;


    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.df_activity_retrieve_password_second);
        bOkHttpManager = new OkHttpManager(this, bHandler);
        mTimeCount = new TimeCount(120000, 1000);
        initData();
    }

    @Override
    protected void initViews() {
        setTitle("找回密码").setRightImage(0).setRightText(null);
        tv_login_code = (EditText) findViewById(R.id.tv_login_code);
        et_login_password = (EditText) findViewById(R.id.et_login_password);
        tv_login_send_code = (TextView) findViewById(R.id.tv_login_send_code);
    }

    @Override
    protected void initEvents() {
        findViewById(R.id.tv_login_in).setOnClickListener(this);
        tv_login_send_code.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_login_in:
                //完成
                submit();
                break;
            case R.id.tv_login_send_code:
                //发送验证吗
                sendVerifyCode();
                break;
        }
    }

    private void initData() {
        Map<String, String> body = new HashMap<>();
//            token	String	登录成功返回的token
        body.put("token", DataManger.getInstance().getUserInfo().token);
//            id	String	家庭成员id
        bOkHttpManager.httpRequest(Constant.RequestType.SEND_MESSAGE, Constant.Http.POST, body, false, new
                TypeToken<Response<? extends IIrrigatedAreaFilter, Void>>() {
                }.getType(), true, false);
    }


    /**
     * 发送验证码
     */
    private void sendVerifyCode() {
        tv_login_send_code.setEnabled(false);
        if (StrUtil.isEmpty(getIntent().getStringExtra(PHONE_NUMBER)) || getIntent()
                .getStringExtra(PHONE_NUMBER).length() < 11) {
            BToast.getInstance().show("手机号码有误");
            tv_login_send_code.setEnabled(true);
        } else {
            mTimeCount.start();
            Map<String, String> body = new HashMap<>();
            body.put("mobile", getIntent().getStringExtra(PHONE_NUMBER));
            bOkHttpManager.httpRequest(Constant.RequestType.SEND_MESSAGE, Constant.Http.POST, body, false, new TypeToken<Response<Void, Void>>() {
            }.getType(), true, false);
        }
    }

    /**
     * 提交
     */
    private void submit() {
        Map<String, String> body = new HashMap<>();
//        token	String	登录成功返回的token
        body.put("token", DataManger.getInstance().getUserInfo().token);

        bOkHttpManager.httpRequest(Constant.RequestType.SEND_MESSAGE, Constant.Http.POST, body, false, new
                TypeToken<Response<Void, Void>>() {
                }.getType(), true, false);
    }

    @Override
    protected void onPostHandle(int requestType, Object response, boolean error, int errorCode, Object obj1, Object obj2) {
        super.onPostHandle(requestType, response, error, errorCode, obj1, obj2);
        if (error) {
            BToast.getInstance().show(getResources().getString(R.string.abnormal));
        } else {
            if (response == null) {
                BToast.getInstance().show(getResources().getString(R.string.datafailure));
            } else {
                if (requestType == Constant.RequestType.SEND_MESSAGE) {
                    Response<Map<String, String>, Void> result = (Response<Map<String, String>, Void>) response;
                    if (result.getCode() == 0) {
                        onBackPressed();
                        BToast.getInstance().show(result.getMsg());
                    } else {
                        BToast.getInstance().show(result.getMsg());
                    }

                }
            }
        }
    }

    private class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {// 计时完毕
            tv_login_send_code.setText("获取验证码");
            tv_login_send_code.setEnabled(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程
//            L.e("重发(" + millisUntilFinished / 1000 + "s)");
            tv_login_send_code.setEnabled(false);//防止重复点击
            tv_login_send_code.setText("重新获取 " + millisUntilFinished / 1000 + "s");
        }
    }
}
